####################################
# RDD plots gender other contracts
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)
library(gridExtra)

################
# Prepare data 
################

# read data
load("~/Dropbox/Gender Chile/08_replication/gender_chile_2020aug28.RData")  
names(d)

#########################
# RDD plots
#########################

# Female permanent
summary(rdrobust(d$female_permanent,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$female_permanent, x = d$margin,  h= 0.091, nbins = 1000, subset = -0.091 <= d$margin & d$margin <= 0.091, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-74.014,-15.180]",
        y.label = "Women long-term employees", x.label = "Women candidates' margin of victory"))


# Male permanent 
summary(rdrobust(d$male_permanent,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$male_permanent, x = d$margin,  h= 0.140, nbins = 1000, subset = -0.140 <= d$margin & d$margin <= 0.140, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-66.792,-4.961]",
        y.label = "Men long-term employees", x.label = "Women candidates' margin of victory"))

# Share female permanent 
summary(rdrobust(d$sharefemale_permanent,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$sharefemale_permanent, x = d$margin,  h= 0.119, nbins = 1000, subset = -0.119 <= d$margin & d$margin <= 0.119, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.035,0.140]",
        y.label = "Share of women long-term employees", x.label = "Women candidates' margin of victory"))

# Female temporary
summary(rdrobust(d$female_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$female_temporary, x = d$margin,  h= 0.089, nbins = 1000, subset = -0.089 <= d$margin & d$margin <= 0.089, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-23.639,-4.678]",
        y.label = "Women short-term employees", x.label = "Women candidates' margin of victory"))


# Male temporary
summary(rdrobust(d$male_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$male_temporary, x = d$margin,  h= 0.075, nbins = 1000, subset = -0.075 <= d$margin & d$margin <= 0.075, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0,300), title = "Robust CI: [-55.709,-14.962]",
        y.label = "Men short-term employees", x.label = "Women candidates' margin of victory"))

# Share female temporary
summary(rdrobust(d$sharefemale_temporary,d$margin,cluster = d$cluster,all=TRUE))

(rdplot(y = d$sharefemale_temporary, x = d$margin,  h= 0.122, nbins = 1000, subset = -0.122 <= d$margin & d$margin <= 0.122, 
        binselect="esmv", kernel="triangular", p=1, y.lim = c(0, 1), title = "Robust CI: [0.036,0.127]",
        y.label = "Share of women short-term employees", x.label = "Women candidates' margin of victory"))
